package com.example.springboot_redis.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springboot_redis.Dao.Po.StudentPo;
import com.example.springboot_redis.mapper.StudentMapper;
import com.example.springboot_redis.service.RedisServiceI;
import com.example.springboot_redis.service.StudentI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentImpl implements StudentI {

    private final StudentMapper studentMapper;
    private final RedisServiceI redisService;

    public StudentImpl(StudentMapper studentMapper, RedisServiceI redisService) {
        this.studentMapper = studentMapper;
        this.redisService = redisService;
    }

    @Override
    public StudentPo getStudentById(int id) {
        // Implementation logic to retrieve a student by ID
        return null; // Placeholder return statement
    }

    @Override
    public boolean addStudent(StudentPo studentPo) {
        // Implementation logic to add a new student
        return false; // Placeholder return statement
    }

    @Override
    public boolean updateStudent(StudentPo studentPo) {
        // Implementation logic to update an existing student
        return false; // Placeholder return statement
    }

    @Override
    public boolean deleteStudent(int id) {
        // Implementation logic to delete a student by ID
        return false; // Placeholder return statement
    }

    @Override
    public List<StudentPo> getAllStudents() {
        LambdaQueryWrapper<StudentPo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(StudentPo::getId, StudentPo::getName);
        List<StudentPo> students = studentMapper.selectList(queryWrapper);
        for (StudentPo studentPo : students) {
            String key = String.valueOf(studentPo.getId());
            if (!redisService.exists(key)) {
                redisService.set(key, studentPo);
            } else {
                StudentPo cachedStudent = (StudentPo) redisService.get(key);
                if (cachedStudent != null) {
                    studentPo.setName(cachedStudent.getName());
                }
            }
        }

        return students; // Placeholder return statement
    }
}
